/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Core;

import Controller.DocGiaController;
import Controller.NgonNguController;
import Controller.NhaXuatBanController;
import Controller.SachController;
import Controller.TacGiaController;
import Controller.TheLoaiController;
import DataAccess.DocGia;
import DataAccess.Sach;
import DataAccess.TacGia;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.HashMap;
//import javax.management.Query;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
 *
 * @author My PC
 */
public abstract class CoreManager<T> {
  //  protected int currRow, totalRows;
    public String TableName;
    public String[] columnNames;
    protected Query query;
    protected Session session;
    protected List<Object> list;
    private Criteria crit;
    public abstract void SetcolumnNames();
    /**
     *
     */
    protected ArrayList<T> lstSearch=new ArrayList<T>();
    
    public CoreManager()
    {
        this.session = HibernateUtil.getSessionFactory().openSession();
      //  GetAllData(TableName);
    }
    
    public void Insert(T obj)
    {
        
        session.beginTransaction();
        session.save(obj);
        session.getTransaction().commit();
    }
    
    public void Update(T obj)
    {
        session.beginTransaction();
        session.update(obj);
        session.getTransaction().commit();
    }
    public void UpdateSoLuong(int _sachID,int ss)
    {
        session.getTransaction().begin();
        Query query = session.createQuery("update Sach set SoSach = "+String.valueOf(ss) + " where SachID = "+String.valueOf(_sachID));
        int result = query.executeUpdate();
        session.getTransaction().commit();
    }
    public void DeleteAll(String _name)
    {
        session.getTransaction().begin();
        Query query = session.createQuery("DELETE FROM "+_name);
        int result = query.executeUpdate();
        session.getTransaction().commit();
    }
    public void Delete(T obj)
    {
        session.beginTransaction();
        session.delete(obj);
        session.getTransaction().commit();
    }
    
    public T GetObject(int id)
    {
        return (T)list.get(id);
    }
    
    public void GetAllData(String TableName)
    {
        try
        {
            this.session.beginTransaction();
        this.query=this.session.createQuery("from "+TableName);
        System.out.println(query);
        this.session.getTransaction().commit();
        list=this.query.list();
    //    System.out.println(lst);
          //  this.session.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        finally{
           // this.session.close();
        }
//        return lst;
    }
    
    public List GetData(String strQuery)
    {
        List<TacGia> lst=null;
        try
        {
            this.session.beginTransaction();
        this.query=this.session.createQuery(strQuery);
        System.out.println(query);
        this.session.getTransaction().commit();
        lst=this.query.list();
    //    System.out.println(lst);
          //  this.session.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        finally{
           // this.session.close();
        }
        return lst;
    }
    
    protected void createCriteria(Class type){
        //Open a transaction, initialize new value for crit
        this.session.beginTransaction();
        crit = this.session.createCriteria(type);
        
        //close transaction
        this.session.getTransaction().commit();        
    }
        // public ArrayList<T> Search()
    public T GetObjByID(int id,String TableName)
    {
        T t= null;
        try
        {
            this.session.beginTransaction();
        this.query=this.session.createQuery("from "+TableName+" where "+TableName+"ID = "+String.valueOf(id));
        System.out.println(query);
        this.session.getTransaction().commit();
        if(!query.list().isEmpty())
        t=(T)this.query.list().get(0);
    //    System.out.println(lst);
          //  this.session.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        finally{
           // this.session.close();
        }
        return t;
    }
    
    
    public T GetObjByNam(String TableName,String name)
    {
        T t= null;
        try
        {
            this.session.beginTransaction();
        this.query=this.session.createQuery("from "+TableName+" where "+ " Ten"+TableName+"="+name);
        System.out.println(query);
        this.session.getTransaction().commit();
        if(!query.list().isEmpty())
        t=(T)this.query.list().get(0);
    //    System.out.println(lst);
          //  this.session.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        finally{
           // this.session.close();
        }
        return t;
    }

}
    
